package com.binyouwei.designpatterns.structural_type.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 宾有为
 * Created on 2024/7/15 16:45
 * Social homepage: https://blog.csdn.net/baidu_41616022
 * @desc 动态代理 - 中介
 **/
public class ProxyDynamicAgent implements InvocationHandler {
    private Object target;

    public ProxyDynamicAgent(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Before method call");
        Object result = method.invoke(target, args);
        System.out.println("After method call");
        return result;
    }
}
